/**
 * Created by Administrator on 2016/7/6.
 */
"use strict";
swan_pc
    .controller("TaskListController",function($scope,Task,$rootScope,TimeExchange,$state,Clip){
        $scope.task=[];
        $scope.ShowFlag=[];
        $scope.init_show=function(){
            //console.log("length:"+$scope.tasks.length);
            for(var i=0;i<$scope.tasks.length;i++){
                var show={"key":i,"value":true};
                $scope.ShowFlag.push(show);
            }
            //console.log(show);
        }
        $scope.change_show=function(option){
            //console.log("change:"+option);
            angular.forEach($scope.ShowFlag,function(data,index,array){
                //console.log("key and option:"+data.key+" "+option);
                if(data.key==option){
                    //console.log("change is run");
                    //data.value==true?false:true;
                    if(data.value==true){
                        data.value=false;
                    }else{
                        data.value=true;
                    }
                    //console.log(data.value);
                }
            })
        }
        $scope.tasks=[];
        Task.query(function(result){
            $scope.task=result;
            var company="";
            var companyId="";
            var opp="";
            var oppId="";
            if($rootScope.toState.name=="companytask") {
                angular.forEach($scope.task, function (data, index, array) {
                    if(data.relatedCompany!=""&&data.relatedCompany!=null) {
                        if ($scope.presentCompanyInfo.id != "" && $scope.presentCompanyInfo.id != null) {
                            company = data.relatedCompany.split(":");
                            companyId = company[0];
                            if (companyId == $scope.presentCompanyInfo.id) {
                                $scope.tasks.push(data);
                            }
                        }
                    }
                })
            } else if($rootScope.toState.name=="opptask") {
                angular.forEach($scope.task, function (data, index, array) {
                    if(data.relatedOpp!=""&&data.relatedOpp!=null) {
                        if ($scope.presentOppInfo.id != "" && $scope.presentOppInfo.id != null) {
                            opp = data.relatedOpp.split(":");
                            oppId = opp[0];
                            if (oppId == $scope.presentOppInfo.id) {
                                $scope.tasks.push(data);
                            }
                        }
                    }
                })
            }else{
                $scope.tasks=result;
            }
            $scope.init_show();
        })
        $scope.TaskDetailShow=function(id){
            //console.log("TaskDetail is run");
            $rootScope.presentTaskid=id;
            $state.go("taskdetail");
        }
        $scope.TimeShow=function(time){
            if(time==null){
                return "";
            }
            var date=new Date(time);
            var timer=date.toString();
            timer=TimeExchange.StringtoLocalTime(timer);
            //console.log("clipserch "+time + " " + " "+date + " "+ timer);
            timer=timer.split(" ");
            return timer[0];
        };
        $scope.predicate = 'id';
        $scope.reverse = true;
        $scope.page = 1;
        $scope.clips = [];
        Clip.query({
            page: $scope.page - 1,
            size: 500,
            sort: [$scope.predicate + ',' + ($scope.reverse ? 'asc' : 'desc'), 'id']
        }, function (result, headers) {
            $scope.totalItems = headers('X-Total-Count')
            $scope.clips = result;
        })
        $scope.ClipTimeShow=function(time){
            if(time==null){
                return "";
            }
            var date=new Date(time);
            var timer=date.toString();
            timer=TimeExchange.StringtoLocalTime(timer);
            //console.log("clipserch "+time + " " + " "+date + " "+ timer);
            return timer;
        };
        $scope.ClipDetailShow=function(id){
            //console.log("ClipDetailShow is run");
            $rootScope.presentEventid=id;
            $state.go("clipdetail");
        }
        $scope.AddClipToTask=function(id){
            //console.log("AddClipToTask is run");
            $state.go("addcliptotask");
            $rootScope.presentTaskid=id;
        }
        $scope.DeleteClipFromTask=function(id){
            swal({
                title: "确定要删除该日程吗",
                text: "删除后将无法恢复！！",
                type: "warning",
                showCancelButton: true,
                confirmButtonColor: "#DD6B55",
                confirmButtonText: "确认删除",
                cancelButtonText: "取消删除",
                closeOnConfirm: false,
                closeOnCancel: false
            }, function(isConfirm) {
                if (isConfirm) {
                    Clip.delete({clipid:id},
                        function(result){
                            swal("删除成功","已从任务中移除该Clip","success");
                            $state.go("tasklist");
                        },
                        function(result){
                            swal("删除失败"," ","error");
                            $state.go("tasklist");
                        })
                }else
                {     swal("删除取消"," ","error");
                    $state.go("tasklist");
                }
            });
        }
        $scope.AddTaskToDatabase=function(){
            //console.log("AddTaskState:"+$rootScope.toState.name);
            if($rootScope.toState.name=="companytask"){
                $state.go("companyaddtask");
            }else if($rootScope.toState.name=="opptask"){
                $state.go("oppaddtask");
            }else {
                $state.go("addtask");
            }
        }

        //console.log("SkullShowFlag:"+$scope.SkullShowFlag);
        $scope.SkullShow=function(option){
            var count=0;
            for(var i=0;i<$scope.clips.length;i++){
                if($scope.clips[i].individualTaskId==option){
                    //console.log("this is run!!1");
                    count=count+1;
                }
            }
            //console.log("taskid:"+option+"   "+"count:"+count);
            if(count==0){
                return true;
            }else{
                return false;
            }
        }
    })
